<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>个人中心页面</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/forgotPassword.css">
</head>
<body>
<!------IE浏览器判断------>
<!--[if lt IE 9]>
<script type="text/javascript" src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js">
</script>
<![endif]-->
<jsp:include page="head.jsp"></jsp:include>
<!------主内容区------>
<div class="main clearfix">
    <div id="news" class="modal">
        <div class="position">
            <h4 class="clearfix">
                <span class="icon icon1"></span><span> 首页 > 找回密码 </span>
            </h4>
        </div>
        <div class="newsList-title clearfix">
            <h3 class="pull-left">找回密码</h3>
            <i></i>
        </div>
        <div class="new-content">
            <div class="forms">
                <form action="changeSuccess.jsp" onsubmit="checks()" method="post" id="formId">
                    <table>
                        <tr>
                            <th><span>*</span>企业名称：</th>
                            <td><input type="text" id="username" name="company.comp_name" placeholder="请输入企业名称"
                                       onblur="checkUser()">
                            </td>
                            <td class="warning" id="username-warn"></td>
                        </tr>
                        <tr>
                            <th>电子邮箱：</th>
                            <td><input type="text" id="email" name="username" placeholder="请输入企业名称"
                                       value=""  disabled>
                            </td>
                            <td class="warning"></td>
                        </tr>
                        <tr class="codeTime">
                            <th><span>*</span>验&nbsp;&nbsp;证&nbsp;&nbsp;码：</th>
                            <td><input type="text" id="codes" placeholder="请输入验证码" name="code"><input type="button" id="getcodes" class="btns" value="获取验证码" onclick="sendemail(this)"></td>
                        </tr>
                        <tr>
                            <th><span>*</span>密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码：</th>
                            <td><input type="password" id="password" name="company.comp_pwd" placeholder="请输入您的密码" onblur="passWord()">
                            </td>
                            <td class="warning" id="password-warn"></td>
                        </tr>
                        <tr>
                            <th><span>*</span>确认密码：</th>
                            <td><input type="password" id="againword" placeholder="请再次输入密码" onblur="againWord()">
                            </td>
                            <td class="warning" id="againword-warn"></td>
                        </tr>
                    </table>
                    <input type="button" value="确认修改" class="submits" onclick="resetpwd()">
                </form>
            </div>
        </div>
    </div>
</div>
<div class="covers"></div>

<!------页脚------>
<jsp:include page="foot.jsp"></jsp:include>
<!------JS部分------>
<script>
    var countdown=60;
    function settime(obj) {
        if (countdown == 0) {
            obj.removeAttribute("disabled");
            obj.value="获取验证码";
            countdown = 60;
            return;
        } else {
            obj.setAttribute("disabled", true);
            obj.value="重新发送(" + countdown + ")";
            countdown--;
        }
        setTimeout(function() {
                settime(obj) }
            ,1000)
    }
    var flag = {};
    function checks() {
        var reg1=new RegExp("^.{3,20}$");
        var reg2=new RegExp("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$");
        var flags;
        if ($(" #username").val() && reg1.test($("#username").val()) &&
            $("#email").val() && reg2.test($(" #email").val()) && $("#codes").val()) {
            alert("修改成功");
            flags= true;
            return true;
        }
        else{
            alert("检查是否有误");
            flags= false;
            return false;
        }


    }
    function passWord() {
        var reg = new RegExp("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$");
        check("password", reg, "password-warn", "密码至少输入6-16个字符，请使用英文字母和数字组合密码");

    }
    function againWord() {
        var flag;
        var rp = document.getElementById("againword").value;
        var ps = document.getElementById("password").value;
        var oSpanNodes = document.getElementById("againword-warn");
        if (rp == ps) {
            oSpanNodes.innerHTML ="";
            flag = true;
        }
        else {
            oSpanNodes.innerHTML = "请再次确认密码是否一致".fontcolor("#c93d00");
            flag = false;
        }
        return flag;
    }
    function check(id, reg, spanId, errorInfo) {
        var val = document.getElementById(id).value;
        var oSpanNode = document.getElementById(spanId);
        if (reg.test(val) && val != "") {
            oSpanNode.innerHTML = "";
            flag = true;
        } else {
            oSpanNode.innerHTML = errorInfo.fontcolor("#c93d00");
            flag = false;
        }

        return flag;
    }
    function emailSimple() {
        $("#email").val($("#email").val().substring(0, 3) + "****" + $("#email").val().substring(7, 25));

    }
    //发送邮件
    function sendemail(obj){
    	var name = $("#username").val();
    	$.post("company_findPWD?company.comp_name="+name,function(){
    	});
    	settime(obj);
    }
    //重置密码
    function resetpwd(){
    	//获取表单数据
    	var data = $("#formId").serialize();
    	$.post("company_resetpwd?"+data,function(data){
    		if(data){
    			alert(data);
    		}else{
    			$("#formId").submit();
    		}
    	});
    }
    
    emailSimple();
    function checkUser() {
        var reg = new RegExp("^.{3,20}$");
        check("username", reg, "username-warn", "请填写您的公司全称");
        //发送ajax请求
        var a = $("#username").val();
        $.post("company_findByName?company.comp_name="+a,function(data){
        	$("#email").val(data);
        	var str = new RegExp("@");
        	$("#getcodes").attr('disabled','disabled');
        	if(str.test(data)){
        		emailSimple();
        		$("#getcodes").removeAttr('disabled');
        	}
        });
    }
</script>
</body>
</html>